﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OFB.BLL.Context;
using OFB.Domain;
using System.Data;

namespace OFB.BLL.Repository
{
   public class CriteriaBLL : ICriteriaBLL
    {
        private readonly CriteriaContext context;         
        public CriteriaBLL()
        {
            context = new CriteriaContext();
        }

        public bool CriteriaUpdateable(int criteriaId)
        {
            IDataParameter rowEffected = context.Criteria.BuildParameter("RowEffect", DbType.Int32, ParameterDirection.Output);

            context.Criteria.Execute("Criteria_Updateable", context.Criteria.BuildParameter("CriteriaId", DbType.Int32, criteriaId),

                                                         rowEffected);
            if ((int)rowEffected.Value == 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// Get all Criteria
        /// </summary>
        /// <returns></returns>
        public ICollection<Criteria> FindAll(int startRow, int maxRow, out int totalRow)
        {
            IDataParameter para = context.Criteria.BuildParameter("totalRows", DbType.Int32, ParameterDirection.Output);
            totalRow = 0;
            ICollection<Criteria> lst = context.Criteria.Init("Criteria_Search", context.Criteria.BuildParameter("startRowIndex", DbType.Int32, startRow),
                                     context.Criteria.BuildParameter("maximumRows", DbType.Int32, maxRow),
                                     para)
                                               .MapCollection<Criteria>()
                                               .GetCollection();
            totalRow = Convert.ToInt32(para.Value);
            return lst;
        }

        /// <summary>
        /// Fileter Criteria
        /// </summary>
        /// <param name="status"></param>
        /// <param name="totalRow"> </param>
        /// <param name="criteriaName"></param>
        /// <param name="criteriaId"></param>
        /// <param name="searchString"></param>
        /// <param name="startRow"> </param>
        /// <param name="maxRow"> </param>
        /// <returns></returns>
        public ICollection<Criteria> Filter(bool? status, int startRow, int maxRow, out int totalRow, string criteriaName = null,string criteriaId = null, string searchString = null)
        {
            IDataParameter para = context.Criteria.BuildParameter("totalRows", DbType.Int32, ParameterDirection.Output);
            totalRow = 0;
            ICollection<Criteria> lst = context.Criteria.Init("Criteria_Search",
                                      context.Criteria.BuildParameter("Status", DbType.Boolean, status),
                                      context.Criteria.BuildParameter("CriteriaName", DbType.String, criteriaName),
                                      context.Criteria.BuildParameter("CriteriaId", DbType.Int32, criteriaId),
                                      context.Criteria.BuildParameter("SearchString", DbType.Int32, searchString),
                                      context.Criteria.BuildParameter("startRowIndex", DbType.Int32, startRow),
                                      context.Criteria.BuildParameter("maximumRows", DbType.Int32, maxRow),
                                      para)
                     .MapCollection<Criteria>()
                    .GetCollection();
            totalRow = Convert.ToInt32(para.Value);
            return lst;
        }


        /// <summary>
        /// Change status of Criteria
        /// </summary>
        /// <param name="criteriaId"></param>
        public void ChangeStatus(int criteriaId)
        {
            context.Criteria.Execute("Criteria_ChangeStatus", context.Criteria.BuildParameter("CriteriaId", DbType.Int32, criteriaId));
        }   

       /// <summary>
       /// Find all criteria
       /// </summary>
       /// <returns></returns>  

        public ICollection<Domain.Criteria> FindAll()
        {
            return context.Criteria.Init("CriteriaSearch", null)
                                                .MapCollection<Criteria>()
                                                .GetCollection();
        } 

       /// <summary>
       /// Create criteria method
       /// </summary>
       /// <param name="criteriaName"></param>
       /// <param name="description"></param>
       /// <param name="status"></param>
       /// <returns></returns>

        public int CreateCriteria( string criteriaName, string description, bool status)
        {
            IDataParameter criteriaIdParam = context.Criteria.BuildParameter("CriteriaId", DbType.Int32, ParameterDirection.Output);
            context.Criteria.Execute("Criteria_Create", context.Criteria.BuildParameter("CriteriaName", DbType.String, criteriaName),
                                                        context.Criteria.BuildParameter("Description", DbType.String, description),
                                                        context.Criteria.BuildParameter("IsDisabled", DbType.Boolean, status),
                                                        criteriaIdParam);
            return (int)criteriaIdParam.Value;
        }

       /// <summary>
       /// Update criteria method
       /// </summary>
       /// <param name="criteriaId"></param>
       /// <param name="criteriaName"></param>
       /// <param name="description"></param>
       /// <returns></returns>

        public int UpdateCriteria(int criteriaId, string criteriaName, string description)
        {
            IDataParameter rowEffectedParam = context.Criteria.BuildParameter("RowEffected", DbType.Int32,ParameterDirection.Output);
            context.Criteria.Execute("Criteria_Update", context.Criteria.BuildParameter("CriteriaId",DbType.Int32, criteriaId),
                                                        context.Criteria.BuildParameter("CriteriaName", DbType.String, criteriaName),
                                                        context.Criteria.BuildParameter("Description", DbType.String, description),
                                                        rowEffectedParam);
            return (int)rowEffectedParam.Value;
        } 

       /// <summary>
       /// Find Criteria Name
       /// </summary>
       /// <param name="criteriaName"></param>
       /// <returns></returns>

        public ICollection<Criteria> FindCriteriaName(string criteriaName)
        {
            return context.Criteria.Init("Criteria_IsExistedCriteriaName",
                                         context.Criteria.BuildParameter("CriteriaName", DbType.String, criteriaName))
                                         .MapCollection<Criteria>()
                                         .GetCollection();
        }

       /// <summary>
       /// Find Criteria Name by Id
       /// </summary>
       /// <param name="criteriaId"></param>
       /// <returns></returns>

        public Criteria FindCriteriaById(int criteriaId)
        {
            return
                context.Criteria.Init("Criteria_GetCriteriaById",                                      
                                      context.Criteria.BuildParameter("CriteriaId", DbType.Int32, criteriaId))
                                      .Map()
                                      .Get();
        }

        /// <summary>
        /// Find Exsisted Criteria for Update
        /// </summary>
        /// <param name="criteriaName"></param>
        /// <param name="criteriaId"></param>
        /// <returns></returns>
        public ICollection<Criteria> FindCriteriaNameUpdate(string criteriaName, int criteriaId)
        {
            return context.Criteria.Init("Criteria_IsExistedCriteriaNameNotThisId",
                                          context.Criteria.BuildParameter("CriteriaName", DbType.String, criteriaName),
                                          context.Criteria.BuildParameter("CriteriaId", DbType.Int32, criteriaId))
                                          .MapCollection<Criteria>()
                                          .GetCollection();
        }
    }
}
